﻿using CollegeSim.CourseScheduler.Presentation.Facade;
using System;
using System.Windows.Forms;

namespace CollegeSim.CourseScheduler.Presentation
{
    // Author: Ryan Redburn
    // Project: Course Scheduler
    // Date: 2/16/2014

    #region [ Updates ]

    // Author: Ryan Redburn
    // Date: 5/19/2014
    // Revisions: Removed use of course index as means of locating course information (corresponding to SchedulerFacade changes).

    #endregion

    public partial class FormCourseCreate : Form
    {
        #region [ Fields ]

        private readonly SchedulerFacade _facade;
        private readonly bool _isUpdate;
        private int _courseUpdateID;

        #endregion

        #region [ Constructors ]

        // Initialize form resources
        // Determine whether form instance is used for updating or creation
        public FormCourseCreate(SchedulerFacade facade, bool isUpdate, int courseID = 0)
        {
            InitializeComponent();
            _facade = facade;
            _isUpdate = isUpdate;

            if (courseID != 0)
                _courseUpdateID = courseID;
        }

        #endregion

        #region [ Events ]

        // Prepare form resources
        private void frmCreateCourse_Load(object sender, EventArgs e)
        {
            // End method if creation instance
            if (!_isUpdate) return;

            btnCreate.Text = "Update";

            if (_courseUpdateID == 0)
            {
                // If udpate instance open course select form
                using (var frmCourseSelect = new FormCourseSelect(_facade))
                {
                    frmCourseSelect.ShowDialog();

                    // If the user selected a course, retrieve the index value
                    if (frmCourseSelect.DialogResult == DialogResult.OK)
                    {
                        _courseUpdateID = frmCourseSelect.SelectionID;
                    }
                    else
                    {
                        Close();
                    }
                }
            }

            // Retrieve the selected course from the facade
            var courseInfo = _facade.GetCourseFromTempStore(_courseUpdateID);

            // Populate form fields with course information
            txtCourseID.Text = courseInfo[0];
            txtSubject.Text = courseInfo[1];
            txtCourseNum.Text = courseInfo[2];
            txtTitle.Text = courseInfo[3];
            numCredits.Value = int.Parse(courseInfo[4]);
            numContact.Value = int.Parse(courseInfo[5]);

            if (courseInfo[6] == "Yes" && courseInfo[7] == "No")
            {
                radOnline.Checked = true;
            }
            else if (courseInfo[7] == "Yes" && courseInfo[6] == "No")
            {
                radHybrid.Checked = true;
            }
            else
            {
                radClassroom.Checked = true;
            }
        }

        // Passes data to the facade to start the course creation process
        private void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                if (!_isUpdate) // Determine form instance type
                {
                    // Create course
                    _facade.CreateCourse(txtSubject.Text, txtCourseNum.Text, txtTitle.Text,
                    (int)numCredits.Value, (int)numCredits.Value, radOnline.Checked, radHybrid.Checked);

                    // Show success message and close
                    MessageBox.Show("The course was successfully added.", "Successful Addition");
                    Close();
                }
                else
                {
                    // Update course
                    _facade.UpdateCourse(_courseUpdateID, txtSubject.Text, txtCourseNum.Text,
                        txtTitle.Text, (int)numCredits.Value, (int)numCredits.Value, radOnline.Checked,
                        radHybrid.Checked);

                    // Show success message and close
                    MessageBox.Show("The selected course was successfully updated.", "Successful Update");
                    Close();
                }
            }
            catch (ArgumentException)
            {
                MessageBox.Show("Please make sure to input values for all fields.", "Missing Information");
            }
        }

        // Resets data fields
        private void btnClear_Click(object sender, EventArgs e)
        {
            txtCourseID.Clear();
            txtSubject.Clear();
            txtCourseNum.Clear();
            txtTitle.Clear();
            numCredits.ResetText();
            numContact.ResetText();
            radClassroom.Checked = true;
        }

        // Closes the form
        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion
    }
}
